package com.czy.project.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czy.project.entity.User;
import com.czy.project.exceptionHandler.MyException;
import com.czy.project.service.TestService;
import com.czy.project.util.DataResult;
import com.czy.project.util.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/********************************************************************
 * @brief
 * @version 0.1
 * @date 2022年09月06日 21:46
 * @author changziyang
 ********************************************************************/
@Slf4j
@Api(tags = "用户 前端控制器")
@RestController
@RequestMapping("/user")
public class TestController {

    @Autowired
    private TestService testService;

    @ApiOperation(value = "查询所有用户")
    @GetMapping("findAll")
    public DataResult findAll() {
        try{
            log.info("查询所有用户");
            log.error("查询所有用户");
            return DataResult.builder()
                    .data(testService.list())
                    .successBuild();
        }catch (Exception e){
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("查询所有用户失败")
                    .build();
        }
    }

    @ApiOperation(value = "通过id逻辑删除用户")
    @DeleteMapping("deleteById/{id}")
    public DataResult deleteById(@ApiParam(name = "id", value = "讲师id", required = true) @PathVariable String id) {
        try{
            return DataResult.builder()
                    .data(testService.removeById(id))
                    .successBuild();
        }catch (Exception e){
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .params(id)
                    .message("查询所有用户失败")
                    .build();
        }

    }

    @ApiOperation(value = "分页查询讲师")
    @GetMapping("pageTeacher/{current}/{limit}")
    public DataResult pageTeacher(@ApiParam(name = "current", value = "当前页码数", required = true) @PathVariable Long current,
                                  @ApiParam(name = "limit", value = "每页显示条数", required = true) @PathVariable Long limit) {

        try{
            //创建分页对象
            //调用方法实现，底层将查询出的数据封装到pageTeacher对象中
            Page<User> pageTeacher = testService.page(new Page<>(current, limit), null);

            Map<String, Object> resultMap = new HashMap<>(2);
            resultMap.put("total", pageTeacher.getTotal());//总记录数
            resultMap.put("data", pageTeacher.getRecords());
            return DataResult.builder()
                    .data(resultMap)
                    .successBuild();
        }catch (Exception e){
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("条件分页查询讲师失败")
                    .params(ArrayUtil.toString(new Object[]{current,limit}))
                    .build();
        }
    }

}
